package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.ImageBean;
import com.hxkcloud.patient.utils.DpToPxUtils;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.views.SuperGridView;

import java.util.ArrayList;
import java.util.List;


/**
 * 出院小结图片适配器
 * Created by Administrator on 2017/1/12.
 */

public class ImageSupGridAdapterchart extends SuperGridView.Adapter {
    private List<ImageBean> imageBeen;
    private Context mContext;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;
    private int type = 0;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 设备屏幕宽度（单位像素）
     */
    private int weight = 0;

    public ImageSupGridAdapterchart(List<ImageBean> pushRecordBeanList, Context mContext, int type) {
        this.imageBeen = pushRecordBeanList;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        int mWidth = dm.widthPixels;
        weight = (int) ((mWidth - DpToPxUtils.dip2px(mContext, 40)) / 3.0);
        this.type = type;
    }


    @Override
    public int getCount() {
        return imageBeen.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position) {
        View view = inflater.inflate(R.layout.adapter_image_chat, null);

        ImageHolder imageHolder = new ImageHolder(view);
        if (type==0){
            imageHolder.car_view.setBackgroundResource(R.drawable.icon_chat_right_back_green);
        }else {
            imageHolder.car_view.setBackgroundResource(R.drawable.icon_chat_left_back_glay);
        }
        imageHolder.car_view.setLayoutParams(new LinearLayout.LayoutParams(weight, weight));
        // 0表示是添加按钮,1表示读取本地图片,2表示网络
        if (imageBeen.get(position).getImageType() == 0) {
            ImageLoader.getInstance().displayImage("drawable://" + R.drawable.add_icon_new,
                    imageHolder.image_icon, ImageOptionUtils.getInstance().getRoundPicOptions(DpToPxUtils.dip2px(mContext, 4)));
            // ImageLoader.getInstance().displayImage("file://" + u, getCurIv(), ImageOptionUtils.getInstance().getSimplePicOptions());
        } else if (imageBeen.get(position).getImageType() == 1) {
            ImageLoader.getInstance().displayImage("file://" + imageBeen.get(position).getImage(),
                    imageHolder.image_icon, ImageOptionUtils.getInstance().getRoundPicOptions(DpToPxUtils.dip2px(mContext, 4)));
        } else {
            ImageLoader.getInstance().displayImage(imageBeen.get(position).getImage(), imageHolder.image_icon,
                    ImageOptionUtils.getInstance().getRoundPicOptions(DpToPxUtils.dip2px(mContext, 4)));
        }
        if (onItemClickListener != null) {
            imageHolder.car_view.setTag(position);
            imageHolder.car_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int index = (int) v.getTag();
                    onItemClickListener.onItemClick(v, index);
                }
            });
        }
        return view;
    }


    public ImageBean getImageBeen(int position) {
        return imageBeen.get(position);
    }
    public  List<String> getImageBeen() {
        List<String> list=new ArrayList<>();
        for (int i = 0; i < imageBeen.size(); i++) {
            list.add(imageBeen.get(i).getImage());
        }
        return list;
    }

    class ImageHolder extends RecyclerView.ViewHolder {
        public ImageView image_icon;
        public LinearLayout car_view;

        public ImageHolder(View itemView) {
            super(itemView);
            image_icon = (ImageView) itemView.findViewById(R.id.image_icons);
            car_view = (LinearLayout) itemView.findViewById(R.id.car_views);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
